import {ref} from 'vue'
import request from '@/base/request'
import router from '@/router'
import { ElMessage } from 'element-plus'
export default function(){

    
    const codeInfo=ref<string>()
    const loginParm=ref({
        username: "",
        password: "",
        code: "",
        id:"",
    })


    async function doLogin() {
        if(loginParm.value.username.length<3 || loginParm.value.password.length<3 || loginParm.value.code.length<4 || loginParm.value.id.length<5){
            ElMessage({
                message: '请检查参数是否有误',
                grouping: true,
                type: 'error'
            })
            return
        }
        const response=await request.post(`/user/doLogin`,loginParm.value)
        if(response.data.code===200){
            ElMessage.success("登录成功")
            localStorage.setItem('loginUserId',response.data.data.id)
            router.push("/home")
        }else{

            ElMessage({
                message:`登录失败:${response.data.msg}`,
                grouping: true,
                type: 'error'
            })
        }
    }

    async function getCode() {
        const response=await request.get(`/user/getCodeImage`)
        if(response.data.code===200){
            codeInfo.value=response.data.data.base64String
            loginParm.value.id=response.data.data.id
        }else{
            ElMessage.error(`获取验证码错误:${response.data.msg}，请稍后重试`)
        }
    }



    return {loginParm,codeInfo,doLogin,getCode}


}